package com.tang.model.proxy.dynamicproxy.cglibProxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Author tangyuan2meicloud.com
 * @DATE 2021/8/17 6:37
 * @Desc
 * @Remarks
 */
public class CglibMatchmaker implements MethodInterceptor {


    public Object getInstance(Class<?> clazz) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        before();
        methodProxy.invokeSuper(o, objects);
        after();

        return null;
    }

    private void after() {
        System.out.println("为您筛选");
    }

    private void before() {
        System.out.println("前期准备，提出需求：");
    }
}
